package com.wave.wavesystem.develop.dbsource.controller;

import com.mybatisflex.core.paginate.Page;
import wave.common.pojo.entity.KV;
import wave.common.pojo.entity.Query;
 import wave.common.pojo.entity.Result;
import wave.common.pojo.enums.RStatusEnum;
import com.wave.wavesystem.common.util.Condition;
import com.wave.wavesystem.common.util.Func;
import com.wave.wavesystem.develop.codeGenetrate.mapper.DatabaseMapper;
import com.wave.wavesystem.develop.dbsource.pojo.dto.DbSourceDTO;
import com.wave.wavesystem.develop.dbsource.pojo.entity.DbSourceEntity;
import com.wave.wavesystem.develop.dbsource.pojo.enums.DbDriverTypeEnum;
import com.wave.wavesystem.develop.dbsource.service.DbSourceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 夕下风波
 * @date 2024-12-03 23:03
 * @description 控制层
 */
@RestController
@RequestMapping("/dbSource")
@Tag(name = "数据源接口")
public class DbSourceController {

    @Resource
    private DbSourceService dbSourceService;

    @Resource
    private DatabaseMapper databaseMapper;

    /**
     * 更新
     *
     * @param dbSourceEntity 更新内容
     */
    @Operation(summary = "更新")
    @PostMapping("/update")
    public Result<DbSourceEntity> update(@RequestBody DbSourceEntity dbSourceEntity) {
        dbSourceService.updateById(dbSourceEntity);
        return Result.data(dbSourceEntity, RStatusEnum.SUCCESS);
    }

    /**
     * 新增
     *
     * @param dbSourceDTO 新增实体
     */
    @Operation(summary = "保存")
    @PostMapping("/save")
    public Result<DbSourceEntity> save(@RequestBody DbSourceDTO dbSourceDTO) {
        dbSourceService.save(dbSourceDTO);
        return Result.data(dbSourceDTO, RStatusEnum.SUCCESS);
    }

    /**
     * 详情查询
     *
     * @param dbSourceEntity 查询条件
     */
    @Operation(summary = "详情")
    @GetMapping("/detail")
    public Result<DbSourceEntity> detail(DbSourceEntity dbSourceEntity) {
        return Result.data(dbSourceService.getById(dbSourceEntity.getId()));
    }

    /**
     * 列表查询
     *
     * @param dbSourceEntity 查询条件
     */
    @Operation(summary = "列表")
    @GetMapping("/list")
    public Result<List<DbSourceEntity>> list(DbSourceEntity dbSourceEntity) {
        return Result.data(dbSourceService.list(Condition.getQueryWrapper(dbSourceEntity)));
    }

    /**
     * 条件分页查询
     *
     * @param dbSourceEntity 查询条件
     * @param query          分页条件
     */
    @Operation(summary = "分页")
    @GetMapping("/page")
    public Result<Page<DbSourceEntity>> page(DbSourceEntity dbSourceEntity, Query query) {
        return Result.data(dbSourceService.page(new Page<>(query.getPageNum(), query.getPageSize()),
                Condition.getQueryWrapper(dbSourceEntity)));
    }

    /**
     * 删除/批量删除
     *
     * @param ids id集合
     */
    @Operation(summary = "删除")
    @PostMapping("/remove")
    public Result<Boolean> page(@RequestParam String ids) {
        List<String> strings = Func.strToStringList(ids);
        return Result.status(dbSourceService.removeByIds(strings), RStatusEnum.SUCCESS);
    }

    /**
     * 获取对应数据库下面的所有表
     *
     * @param databaseName 数据库名称
     */
    @Operation(summary = "获取对应数据库中的所有表")
    @GetMapping("/tables/{databaseName}")
    public Result<List<KV<String, String>>> getTablesByDatabaseName(@PathVariable("databaseName") String databaseName) {
        if (Func.isBlank(databaseName))
            return Result.fail("数据库不存在！");
        List<KV<String, String>> tablesByDatabaseName = databaseMapper.getTablesByDatabaseName(databaseName);
        if (Func.isNotEmpty(tablesByDatabaseName)) {
            tablesByDatabaseName.forEach(item -> {
                item.setK(item.getV() + ":" + item.getK());
            });
        }
        return Result.data(tablesByDatabaseName);
    }

    /**
     * 获取支持的驱动版本
     */
    @Operation(summary = "获取数据库驱动类型")
    @GetMapping("/dbDriverTypes")
    public Result<List<String>> getDbDriverTypes() {
        return Result.data(DbDriverTypeEnum.getDbDriverTypes());
    }

}
